package br.com.caiocarrara.goodbuy.infra;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import br.com.caelum.vraptor.ioc.Component;
import br.com.caelum.vraptor.ioc.ComponentFactory;

@Component
public class SessionCreator implements ComponentFactory<Session> {
	
	private final SessionFactory sessionFactory;
	private Session session;
	
	public SessionCreator(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getInstance() {
		return this.session;
	}

	@PostConstruct
	public void init() {
		this.session = this.sessionFactory.openSession();;
	}
	
	@PreDestroy
	public void destory() {
		this.session.close();
	}

}
